--[[=========================================================
	DAMAGE INFO
=========================================================]]--

--[[-----------------------------------------------------]]--
--	Constructor
--[[-----------------------------------------------------]]--
ObjDamageInfo					= class( function( self, id, base, chest, gear, generic, head, leftArm, rightArm, leftLeg, rightLeg, stomach )

	--	Members
	self.TableName				= "DamageInfo"
	self.ID						= ValOrDef( id, "integer", 0 )
	self.BaseDamage				= ValOrDef( base, "integer", 0 )
	self.ChestMultiplier		= ValOrDef( chest, "number", 0 )
	self.GearMultiplier			= ValOrDef( gear, "number", 0 )
	self.GenericMultiplier		= ValOrDef( generic, "number", 0 )
	self.HeadMultiplier			= ValOrDef( head, "number", 0 )
	self.LeftArmMultiplier		= ValOrDef( leftArm, "number", 0 )
	self.RightArmMultiplier		= ValOrDef( rightArm, "number", 0 )
	self.LeftLegMultiplier		= ValOrDef( leftLeg, "number", 0 )
	self.RightLegMultiplier		= ValOrDef( rightLeg, "number", 0 )
	self.StomachMultiplier		= ValOrDef( stomach, "number", 0 )
	
--	SQL management - SERVER only
if SERVER then
	
	--	SQL Insert ( table, ids, contents )
	self.Insert					= function( self )
	
		--	Variables
		local result			= nil
	
		--	Generate a new ID
		if self.ID == 0 then
			self.ID					=
				sql.GetNewID(
					self.TableName,
					FieldInfo( "ID", self.ID, "number" )
				)
		end
		
		--	Insert in DB if we got an ID
		if self.ID != 0 then
			result = sql.Insert(
				self.TableName,
				{ 1 },
				FieldInfo( "ID", self.ID, "number" ),
				FieldInfo( "BaseDamage", self.BaseDamage, "number" ),
				FieldInfo( "ChestMultiplier", self.ChestMultiplier, "number" ),
				FieldInfo( "GearMultiplier", self.GearMultiplier, "number" ),
				FieldInfo( "GenericMultiplier", self.GenericMultiplier, "number" ),
				FieldInfo( "HeadMultiplier", self.HeadMultiplier, "number" ),
				FieldInfo( "LeftArmMultiplier", self.LeftArmMultiplier, "number" ),
				FieldInfo( "RightArmMultiplier", self.RightArmMultiplier, "number" ),
				FieldInfo( "LeftLegMultiplier", self.LeftLegMultiplier, "number" ),
				FieldInfo( "RightLegMultiplier", self.RightLegMultiplier, "number" ),
				FieldInfo( "StomachMultiplier", self.StomachMultiplier, "number" )
			)
		end
	end
	
	--	SQL Update ( table, ids, contents )
	self.Update					= function( self )
	
		--	Variables
		local result			= nil
	
		--	Update in DB
		result = sql.Update(
			self.TableName,
			{ 1 },
			FieldInfo( "ID", self.ID, "number" ),
			FieldInfo( "BaseDamage", self.BaseDamage, "number" ),
			FieldInfo( "ChestMultiplier", self.ChestMultiplier, "number" ),
			FieldInfo( "GearMultiplier", self.GearMultiplier, "number" ),
			FieldInfo( "GenericMultiplier", self.GenericMultiplier, "number" ),
			FieldInfo( "HeadMultiplier", self.HeadMultiplier, "number" ),
			FieldInfo( "LeftArmMultiplier", self.LeftArmMultiplier, "number" ),
			FieldInfo( "RightArmMultiplier", self.RightArmMultiplier, "number" ),
			FieldInfo( "LeftLegMultiplier", self.LeftLegMultiplier, "number" ),
			FieldInfo( "RightLegMultiplier", self.RightLegMultiplier, "number" ),
			FieldInfo( "StomachMultiplier", self.StomachMultiplier, "number" )
		)
	end
	
	--	SQL Delete ( table, ids, id contents )
	self.Delete					= function( self )
	
		--	Variables
		local result			= nil
	
		--	Delete from DB
		result = sql.Delete(
			self.TableName,
			{ 1 },
			FieldInfo( "ID", self.ID, "number" )
		)
		
		--	Reset ID if it was deleted
		if result then
			self.ID					= 0
		end
	end
	
	--	SQL Reload ( table, ids, id contents )
	self.Reload					= function( self, id )
	
		--	Redefinie id if needed
		if IsInt( id ) then
			self.ID				= id
		end
	
		--	Retrieve from DB
		self:FillProperties(
			sql.GetRow(
				self.TableName,
				{ 1 },
				FieldInfo( "ID", self.ID, "number" ),
				FieldInfo( "BaseDamage", self.BaseDamage, "number" ),
				FieldInfo( "ChestMultiplier", self.ChestMultiplier, "number" ),
				FieldInfo( "GearMultiplier", self.GearMultiplier, "number" ),
				FieldInfo( "GenericMultiplier", self.GenericMultiplier, "number" ),
				FieldInfo( "HeadMultiplier", self.HeadMultiplier, "number" ),
				FieldInfo( "LeftArmMultiplier", self.LeftArmMultiplier, "number" ),
				FieldInfo( "RightArmMultiplier", self.RightArmMultiplier, "number" ),
				FieldInfo( "LeftLegMultiplier", self.LeftLegMultiplier, "number" ),
				FieldInfo( "RightLegMultiplier", self.RightLegMultiplier, "number" ),
				FieldInfo( "StomachMultiplier", self.StomachMultiplier, "number" )
			)
		)
	end
	
	--	Assign fields from row
	self.FillProperties			= function( self, row )
		self.Name				= tostring( row["Name"] )
		self.BaseDamage			= tonumber( row["BaseDamage"] )
		self.ChestMultiplier	= tonumber( row["ChestMultiplier"] )
		self.GearMultiplier		= tonumber( row["GearMultiplier"] )
		self.GenericMultiplier	= tonumber( row["GenericMultiplier"] )
		self.HeadMultiplier		= tonumber( row["HeadMultiplier"] )
		self.LeftArmMultiplier	= tonumber( row["LeftArmMultiplier"] )
		self.RightArmMultiplier	= tonumber( row["RightArmMultiplier"] )
		self.LeftLegMultiplier	= tonumber( row["LeftLegMultiplier"] )
		self.StomachMultiplier	= tonumber( row["SomtachMultiplier"] )
	end
	
end	--	End of SQL management

--	Other server functions
if SERVER then

	--	Returns a table for transfers to clients
	self.GetClientTable				= function( self )
		return {
			["ID"]					= self.ID,
			["BaseDamage"]			= self.BaseDamage,
			["ChestMultiplier"]		= self.ChestMultiplier,
			["GearMultiplier"]		= self.GearMultiplier,
			["GenericMultiplier"]	= self.GenericMultiplier,
			["HeadMultiplier"]		= self.HeadMultiplier,
			["LeftArmMultiplier"]	= self.LeftArmMultiplier,
			["RightArmMultiplier"]	= self.RightArmMultiplier,
			["LeftLegMultiplier"]	= self.LeftLegMultiplier,
			["RightLegMultiplier"]	= self.RightLegMultiplier,
			["StomachMultiplier"]	= self.StomachMultiplier
		}
	end

end
	
	--	Returns the damage that should be dealt for the specified hitgroup
	self.GetHitDamage				= function( self, hitGroup )
		
		--	Hitgroup must be an int
		if not IsInt( hitGroup ) then
			DEBUG:ErrMsg( "Hit-group must be a valid integer. Given: " .. tostring( hitGroup ), "ObjDamageInfo.GetDamage" )
			return self.BaseDamage
		end
		
		--	Chest
		if hitGroup == HITGROUP_CHEST then
			return self.BaseDamage * self.ChestMultiplier
		
		--	Gear
		elseif hitGroup == HITGROUP_GEAR then
			return self.BaseDamage * self.GearMultiplier
			
		--	Generic
		elseif hitGroup == HITGROUP_GENERIC then
			return self.BaseDamage * self.GenericMultiplier
			
		--	Head
		elseif hitGroup == HITGROUP_HEAD then
			return self.BaseDamage * self.HeadMultiplier
			
		--	Left arm
		elseif hitGroup == HITGROUP_LEFTARM then
			return self.BaseDamage * self.LeftArmMultiplier
			
		--	Right arm
		elseif hitGroup == HITGROUP_RIGHTARM then
			return self.BaseDamage * self.RightArmMultiplier
			
		--	Left leg
		elseif hitGroup == HITGROUP_LEFTLEG then
			return self.BaseDamage * self.LeftLegMultiplier
			
		--	Right leg
		elseif hitGroup == HITGROUP_RIGHTLEG then
			return self.BaseDamage * self.RightLegMultiplier
			
		--	Unknown hitgroup
		else
			DEBUG:Msg( "Unknown hitgroup: " .. tostring( hitgroup ), "ObjDamageInfo.GetDamage" )
			return self.BaseDamage
		end
	
	end

end)